package operation;

import book.Book;
import book.BookList;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的书的名字");
        String name = scanner.nextLine();
        int size = bookList.getUsedSize();
        int index = 0;
        int i = 0;
        for (; i < size; i++){
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if (i >= size){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int j = index; j < size - 1; j++){
            Book book = bookList.getPos(j + 1);
            bookList.setBook(j, book);
        }
         bookList.setBook(size - 1, null);
         bookList.setUsedSize(size - 1);
           System.out.println("删除成功");
    }
}
